package com.anyisn.flickr.as3.patterns.proxy
{
	import com.adobe.webapis.flickr.AuthResult;
	import com.adobe.webapis.flickr.FlickrService;
	import com.adobe.webapis.flickr.events.FlickrResultEvent;
	
	import flash.events.IOErrorEvent;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.model.vo.ErrorVO;
	import com.anyisn.flickr.as3.FlickrConstants;

	public class FlickrServiceProxy extends Proxy
	{
		public function FlickrServiceProxy(proxyName:String=null, data:Object=null)
		{
			super(proxyName, data);
			service = new FlickrService(FlickrConstants.API_KEY);
			service.secret = FlickrConstants.SECRET;
			service.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
		}
		
		protected function onFlickrError(event:FlickrResultEvent):void {
			var error:ErrorVO = new ErrorVO(event.type, event.data.error.errorMessage, event.data.error.errorCode);
			sendNotification(ShellFacade.ERROR, error);
		}
		
		// IOError Handler
		protected function onIOError(event:IOErrorEvent):void {
			var error:ErrorVO = new ErrorVO(event.type, event.text, event.errorID);
			sendNotification(ShellFacade.ERROR, error);
		}
		
		// Login
		public function login(data:AuthResult):void {
			service.token = data.token;
			service.permission = data.perms;
			loggedIn = true;
		}
		
		public function logout():void {
			service.token = "";
			service.permission = "";
			loggedIn = false;
		}
		
		protected var service:FlickrService;
		protected var loggedIn:Boolean;
	}
}